﻿namespace WMS.FC.Domain.DomainObject.System
{
    /// <summary>
    /// 基础操作结果
    /// </summary>
    public class ActionResultBase
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; private set; } = true;

        private ErrorCode errorCode = ErrorCode.S;

        /// <summary>
        /// 错误代码
        /// </summary>
        public ErrorCode ErrorCode
        {
            get
            {
                return errorCode;
            }
            set
            {
                IsSuccess = value == ErrorCode.S || value == ErrorCode.E001;
                errorCode = value;
            }
        }

        /// <summary>
        /// 信息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 操作描述
        /// </summary>
        protected readonly string actionFailDescription;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="actionDescription">操作描述</param>
        public ActionResultBase(string? actionDescription = null)
        {
            actionFailDescription = string.IsNullOrWhiteSpace(actionDescription) ? "" : $"{actionDescription}失败,";
        }

        /// <summary>
        /// 部分成功
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public ActionResultBase PartSuccess(string message)
        {
            ErrorCode = ErrorCode.E001;
            Message = message;
            return this;
        }

        /// <summary>
        /// 程序异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public ActionResultBase Exception(Exception ex)
        {
            ErrorCode = ErrorCode.E006;
            Message = $"{actionFailDescription}{ex.Message}";
            return this;
        }

        /// <summary>
        /// 参数校验不通过
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public ActionResultBase ArgumentError(string message)
        {
            ErrorCode = ErrorCode.E003;
            Message = $"{actionFailDescription}{message}";
            return this;
        }

        /// <summary>
        /// 业务校验不通过
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public ActionResultBase BusinessError(string message)
        {
            ErrorCode = ErrorCode.E004;
            Message = $"{actionFailDescription}{message}";
            return this;
        }

        /// <summary>
        /// 数据库执行异常
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public ActionResultBase DbError(string message)
        {
            ErrorCode = ErrorCode.E005;
            Message = $"{actionFailDescription}{message}";
            return this;
        }

        /// <summary>
        /// 调用服务失败
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public ActionResultBase Fail(ActionResultBase result)
        {
            ErrorCode = result.ErrorCode;
            Message = $"{actionFailDescription}{result.Message}";
            return this;
        }
    }
}
